﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShopOnline.Models.DAL;
using ShopOnline.Models;
using System.Web.UI.MobileControls;
using ShopOnline.Models.Utils;
using System.Net.Mail;

namespace ShopOnline.Controllers
{
    public class PressController : Controller
    {
        private DAL_Press pressRepository;
        private DAL_Catelogy catelogyRepository;
        private ShopOnlineDBDataContext db;
        private static int presscurent;
         public PressController()
        {
            pressRepository = new DAL_Press();
            catelogyRepository = new DAL_Catelogy();
            db = new ShopOnlineDBDataContext();
        }
        //
        // GET: /Press/
         [Authorize]
        public ActionResult AdminListPress()
        {
            List<Press> listPress = pressRepository.getListPress();
            return View(listPress);
        }

        //
        // GET: /Press/Details/5
         [Authorize]
        public ActionResult Details(int id)
        {
            var press = pressRepository.getPressById(id);
            var catelogyOfPress = catelogyRepository.getCatelogyById(press.catelogyId??0);
            ViewData["cate"] = catelogyOfPress.catelogyName;
            return View(press);
        }

        //
        // GET: /Press/Create
         [Authorize]
        public ActionResult Create()
        {
            //var username = HttpContext.User.Identity.Name;
            ViewData["folderName"] = DateTime.Now.Month;
            var listCatelogy = catelogyRepository.getListCatelogy();
            ViewData["listCatelogy"] = listCatelogy;
            return View();
        } 

        //
        // POST: /Press/Create
         [Authorize]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                Press press = new Press();
                press.body = collection["body"];
                press.curent = false;
                press.dateCreate = DateTime.Now;
                press.description = collection["description"];
                press.imagePath = collection["imagePath"];
                press.show = true;
                press.title = collection["title"];
                press.userCreate = "Admin";
                press.piority =int.Parse(collection["piority"]);
                press.viewCount = 0;
                var cateId = collection["catelogyId"];
                 db = new ShopOnlineDBDataContext();
                 #region upload images
                 Uploader uploader = new Uploader();
                 uploader.UploadPath = Server.MapPath("~\\Content\\PressImages");
                 uploader.IsLowerName = false;
                 uploader.IsEncryptName = false;
                 uploader.IsOverwrite = false;
                 uploader.Prefix = "";
                 uploader.Suffix = "";

                 uploader.AllowedExtensions.Add(".jpg");
                 uploader.AllowedExtensions.Add(".jpeg");
                 uploader.AllowedExtensions.Add(".gif");
                 uploader.AllowedExtensions.Add(".png");

                 bool success = uploader.DoUpload("MyFile");
                 #endregion
                 press.imagePath = uploader.PostedFile.FileName;
                db.Press_add(press.title, press.description, press.body, press.imagePath, press.viewCount, press.dateCreate,press.piority, press.userCreate, true, false,int.Parse(cateId));
                return RedirectToAction("AdminListPress");
            }
            catch
            {
                var listCatelogy = catelogyRepository.getListCatelogy();
                ViewData["listCatelogy"] = listCatelogy;
                return View();
            }
        }
        
        //
        // GET: /Press/Edit/5
  [Authorize]
        public ActionResult Edit(int id)
        {
            ViewData["listCatelogy"] = catelogyRepository.getListCatelogy();
            var press = pressRepository.getPressById(id);
            return View(press);
        }

        //
        // POST: /Press/Edit/5
         [Authorize]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                int cateid= int.Parse(collection["catelogyId"]);
                Press press = pressRepository.getPressById(id);
                press.body = collection["body"];
                press.curent = false;
                press.description = collection["description"];
                //press.imagePath = collection["imagePath"];
                var show= collection["show"];
                if (show=="false")
                {
                    press.show = false;
                }
                else
                {
                    press.show = true;
                }
               
                press.title = collection["title"];
                press.userCreate = "Admin";
                press.piority = int.Parse(collection["piority"]);
                press.viewCount = 0;
                press.catelogyId = cateid;
                
                db.Press_update(press.pressId, press.title, press.description, press.body, press.imagePath, press.viewCount, press.dateCreate,press.piority, press.userCreate, press.show, press.curent,cateid);
                return RedirectToAction("AdminListPress");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Press/Delete/5
  [Authorize]
        public ActionResult Delete(int id)
        {
            var press = pressRepository.getPressById(id);
            return View(press);
        }

        //
        // POST: /Press/Delete/5
 [Authorize]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var press = pressRepository.getPressById(id);
                pressRepository.DeletePress(press);
                return RedirectToAction("AdminListPress");
            }
            catch
            {
                return View();
            }
        }

        //Lien he
        public ActionResult BrowsePress(int id)
        {
            ViewData["state"] = 0;
            var press = pressRepository.getPressById(id);
            var listPress = catelogyRepository.get5ListPressByCateId(press.catelogyId ?? 0, press.pressId);
            var listPressNew = catelogyRepository.get5ListPressNewByCateId();
            ViewData["listPressNew"] = listPressNew;
            ViewData["listPress"] = listPress;
            return View(press);
        }
        //gui phan hoi
        [HttpPost]
        public ActionResult BrowsePress( int id, FormCollection form)
        {
            int state = 0;
            try
            {
                SentMail sendmail = new SentMail();
                sendmail.address = form["address"];
                sendmail.body = form["body"];
                sendmail.dateSent = DateTime.Now;
                sendmail.email = form["email"];
                sendmail.name = form["name"];
                sendmail.phone = form["phone"];
                sendmail.title = form["title"];
                sendmail.show = true;
                DAL_SentMail DalSendmail = new DAL_SentMail();
                DalSendmail.CreateSentMail(sendmail);
                ViewData["state"] = 1;
                var press = pressRepository.getPressById(id);
                var listPress = catelogyRepository.get5ListPressByCateId(press.catelogyId??0, press.pressId);
                var listPressNew = catelogyRepository.get5ListPressNewByCateId();
                ViewData["listPressNew"] = listPressNew;
                ViewData["listPress"] = listPress;
                


                return View(press);
            }
            catch (Exception)
            {
                ViewData["state"] = state;
                var press = pressRepository.getPressById(id);
                var listPress = catelogyRepository.get5ListPressByCateId(press.catelogyId ?? 0, press.pressId);
                ViewData["listPress"] = listPress;
                var listPressNew = catelogyRepository.get5ListPressNewByCateId();
                ViewData["listPressNew"] = listPressNew;
                return View(press);
            }
            
        }

        //hien thi 6 tin tuc co phan trang
        public ActionResult ViewDetail(int?n)
        {
            int count = pressRepository.getListPress().Count();
            ViewData["count"] = count;
             List<Press> listPress = pressRepository.get6PressPaging(n ?? 0, 6);
             return View(listPress);
        }
        public ActionResult ListAllPress()
        {
            var listPress = pressRepository.getListPress();
            ViewData["listPress"] = listPress;
            return View();
        }
 [Authorize]
        public ActionResult EditImage(int id)
        {
            try
            {
                presscurent = id;
                var pro = pressRepository.getPressById(id);
                return View(pro);
            }
            catch (Exception)
            {
                return RedirectToAction("AdminListProduct");
            }

        }
 [Authorize]
        [HttpPost]
        public ActionResult EditImages()
        {
            try
            {
                var press = pressRepository.getPressById(presscurent);
                #region upload images
                Uploader uploader = new Uploader();
                uploader.UploadPath = Server.MapPath("~\\Content\\PressImages");
                uploader.IsLowerName = false;
                uploader.IsEncryptName = false;
                uploader.IsOverwrite = false;
                uploader.Prefix = "";
                uploader.Suffix = "";

                uploader.AllowedExtensions.Add(".jpg");
                uploader.AllowedExtensions.Add(".jpeg");
                uploader.AllowedExtensions.Add(".gif");
                uploader.AllowedExtensions.Add(".png");

                bool success = uploader.DoUpload("MyFile");
                #endregion
                string abc = uploader.PostedFile.FileName;
                db.Press_update(press.pressId, press.title, press.description, press.body, abc, press.viewCount, press.dateCreate, press.piority, press.userCreate, press.show, press.curent, press.catelogyId);
                return RedirectToAction("Edit", new { id = presscurent });
            }
            catch (Exception)
            {
                return RedirectToAction("EditImage", new { id = presscurent });
            }

        }

    }
}
